package com.example.demo_mysql_mybatisplus.util.easyexcel.converters;


import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.HyperlinkData;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.example.demo_mysql_mybatisplus.util.easyexcel.converters.vo.HyperlinkVo;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;

/**
 * 超链接转换器
 */
public class HyperlinkConverter implements Converter<HyperlinkVo> {
    @Override
    public Class<?> supportJavaTypeKey() {
        return HyperlinkVo.class;//对象属性类型
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.EMPTY;//CellData属性类型
    }

    @Override
    public HyperlinkVo convertToJavaData(ReadConverterContext<?> context) {
        ReadCellData readCellData = context.getReadCellData();

        return null;//CellData转对象属性
    }

    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<HyperlinkVo> context) {
        HyperlinkVo cellValue = context.getValue();
        if(null!=cellValue){
            WriteCellData<String> hyperlink = new WriteCellData<>( cellValue.getName() );
            // 设置内容
            HyperlinkData hyperlinkData = new HyperlinkData();
            hyperlinkData.setAddress( cellValue.getAddress() );
            hyperlinkData.setHyperlinkType(HyperlinkData.HyperlinkType.URL);
            hyperlink.setHyperlinkData(hyperlinkData);
            // 设置文字
            WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
            WriteFont contentWriteFont = new WriteFont();
            contentWriteFont.setItalic(true);
            contentWriteFont.setUnderline(Font.U_SINGLE);
            contentWriteFont.setColor(IndexedColors.BLUE.getIndex());
            contentWriteCellStyle.setWriteFont(contentWriteFont);
//            contentWriteCellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
            hyperlink.setWriteCellStyle(contentWriteCellStyle);

            return hyperlink;
        }
        return null;//对象属性转CellData
    }
}